/**
*    CTR - Centro Tecnologia Rocci
*    Open Source Java Classic Game Toolkit
*
*    (C) 2010, CTR - Centro Tecnologia Rocci
*
*    This library is free software; you can redistribute it and/or
*    modify it under the terms of the GNU Lesser General Public
*    License as published by the Free Software Foundation;
*    version 2.1 of the License.
*
*/
package it.ctr.game.toolkit.level.editor;

import it.ctr.game.toolkit.BrickBox;
import it.ctr.game.toolkit.LevelRenderer;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.*;
import com.mojang.mario.*;
import com.mojang.mario.level.Level;



public class LevelViewer extends JComponent implements MouseListener, MouseMotionListener
{
    private static final long serialVersionUID = -1234000000000000013L;

    private LevelRenderer levelRenderer;
    private Level level;

    private int x = -1;
    private int y = -1;
    private BrickViewer levelBrick;

    public LevelViewer(BrickViewer levelBrick)
    {
        this.levelBrick = levelBrick;
        level = new Level(200, 30);
        Dimension size = new Dimension(level.width * Level.BLOCK_SIZE, level.height * Level.BLOCK_SIZE);
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);

        addMouseListener(this);
        addMouseMotionListener(this);
    }
    
    public void setLevel(Level level)
    {
        this.level = level;
        Dimension size = new Dimension(level.width * Level.BLOCK_SIZE, level.height * Level.BLOCK_SIZE);
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        levelRenderer.setLevel(level);
        invalidate();
        updateUI();
        update(getGraphics());
        repaint();
    }
    
    public Level getLevel()
    {
        return level;
    }

    public void addNotify()
    {
        super.addNotify();
        BrickBox.init(getGraphicsConfiguration());
        levelRenderer = new LevelRenderer(level, getGraphicsConfiguration(), level.width * 16, level.height * 16);
    }

    public void paintComponent(Graphics g)
    {
        g.setColor(Color.darkGray);
        g.fillRect(0, 0, level.width * Level.BLOCK_SIZE, level.height * Level.BLOCK_SIZE);
        levelRenderer.render(g, 0, 0);
        g.setColor(Color.BLACK);
        g.drawRect(x * Level.BLOCK_SIZE - 1, y * Level.BLOCK_SIZE - 1, Level.BLOCK_SIZE+1, Level.BLOCK_SIZE+1);
    }

    public void mouseClicked(MouseEvent e)
    {
    }

    public void mouseEntered(MouseEvent e)
    {
    }

    public void mouseExited(MouseEvent e)
    {
        x = -1;
        y = -1;
        repaint();
    }

    public void mousePressed(MouseEvent e)
    {
    	if (isEnabled()) {
            x = e.getX() / Level.BLOCK_SIZE;
            y = e.getY() / Level.BLOCK_SIZE;

            if (e.getButton() == 3) {
            	level.setByteLayer(x, y, levelRenderer.getLayer(), (byte) 0);
            } else {
               	level.setByteLayer(x, y, levelRenderer.getLayer(), levelBrick.selectedBrick);
            }	
            levelRenderer.repaint(x - 1, y - 1, 3, 3);
            repaint();
    	}
    }

    public void mouseReleased(MouseEvent e)
    {
    }

    public void mouseDragged(MouseEvent e)
    {
    	if (isEnabled()) {
	        x = e.getX() / Level.BLOCK_SIZE;
	        y = e.getY() / Level.BLOCK_SIZE;
	        level.setByteLayer(x, y, levelRenderer.getLayer(), levelBrick.selectedBrick);
	        levelRenderer.repaint(x - 1, y - 1, 3, 3);
	        repaint();
    	}
    }

    public void mouseMoved(MouseEvent e)
    {
    	if (isEnabled()) {
	        x = e.getX() / Level.BLOCK_SIZE;
	        y = e.getY() / Level.BLOCK_SIZE;
	        repaint();
    	}
    }

    public void setLayer(int layer)
    {
    	setEnabled(layer != -1);
        levelRenderer.setLayer(layer);
        levelRenderer.setCam(0, 0);
        levelRenderer.repaint(0, 0, level.width, level.height);
    }
}